package com.stormrage.edu.ctrl.index;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.stormrage.edu.serv.adm.AdmAcctServ;
import com.stormrage.edu.serv.index.IdxImageServ;
import com.stormrage.entity.adm.AdmRes;
import com.stormrage.entity.index.IdxImage;
import com.stormrage.entity.rep.ErrorRep;
import com.stormrage.entity.shiro.ShiroAcct;
import com.stormrage.shiro.bind.CurrentUser;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: IdxImageCtrl.java
 * @Package com.stormrage.edu.ctrl.index
 * @Description: 首页广告 Controller
 * @author www.stormragetech.com
 * @date 2016年4月22日
 * @version V1.0
 */
@Controller
@RequestMapping("/idx/img")
public class IdxImageCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(IdxImageCtrl.class);

	@Value("#{constants['html.title']}")
	private String htmlTitle;

	@Value("#{constants['path.root']}")
	private String rootPath;

	@Value("#{constants['path.image']}")
	private String imagePath;

	@Autowired
	private AdmAcctServ admAcctServ;

	@Autowired
	private IdxImageServ idxImageServ;

	@RequiresPermissions("img:edit")
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, Model model) {

		logger.debug(" 【 Controller 】-【 IndexInfoCtrl 】-【 Edit 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Ctrl操作 ------------------
			IdxImage entity = idxImageServ.queryById(id);

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

			model.addAttribute("data", entity);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 EduVideoCtrl 】-【 Edit 】-【 GET 】-【 COMPLETED 】");
		return "index/image/edit";

	}

	@RequiresPermissions("img:edit")
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public String edit(@CurrentUser ShiroAcct curr, IdxImage entity,
			@RequestParam(value = "file", required = false) MultipartFile file, RedirectAttributes redirect) {

		logger.debug(" 【 Controller 】-【 IndexInfoCtrl 】-【 Edit 】-【 GET 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			if (null != file && file.getBytes().length > 0) {
				String path = rootPath + imagePath;
				String fileName = idxImageServ.upload(file, path);
				entity.setUrl(fileName);
			}

			// ------------------ Ctrl操作 ------------------

			idxImageServ.update(entity);
			errMsg = "提示信息：修改首页广告成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = "提示信息：修改首页广告失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/idx/img/edit/{id}";
		}

		logger.debug(" 【 Controller 】-【 EduVideoCtrl 】-【 Edit 】-【 GET 】-【 COMPLETED 】");
		return "redirect:/idx/img/list";
	}

	@RequiresPermissions("img:view")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(@CurrentUser ShiroAcct curr, Model model) {

		logger.debug(" 【 Controller 】-【 IndexInfoCtrl 】-【 List 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Ctrl操作 ------------------
			List<IdxImage> array = idxImageServ.queryList();

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 EduVideoCtrl 】-【 List 】-【 GET 】-【 COMPLETED 】");
		return "index/image/list";

	}

}
